# Copyright 1999-2015 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test essential Machine interface (MI) operations
#
# Verify that, using the MI, we can create, update, delete variables.
#


load_lib mi-support.exp
set MIFLAGS "-i=mi"

gdb_exit
if [mi_gdb_start] {
    continue
}

standard_testfile var-cmd.c

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
    untested mi-var-display.exp
    return -1
}

mi_delete_breakpoints
mi_gdb_reinitialize_dir $srcdir/$subdir
mi_gdb_load ${binfile}

set line_dct_end [gdb_get_line_number "{int a = 0;}"]

mi_create_breakpoint "$srcfile:$line_dct_end" \
    "break-insert operation" \
    -number 1 -func do_children_tests -file ".*var-cmd.c" \
    -line $line_dct_end

mi_run_cmd
mi_expect_stop "breakpoint-hit" "do_children_tests" "" ".*var-cmd.c" \
	$line_dct_end { "" "disp=\"keep\"" } "run to main"

# Prevent symbol on the address being printed.
mi_gdb_test "-gdb-set print symbol off"

#####       #####
#               #
# Display tests #
#               #
#####       #####

# Test: c_variable-6.1
# Desc: create variable bar
mi_create_varobj bar bar "create local variable bar"

# Test: c_variable-6.2
# Desc: type of variable bar
mi_gdb_test "-var-info-type bar" \
	"\\^done,type=\"int\"" \
	"info type variable bar"

# Test: c_variable-6.3
# Desc: format of variable bar
mi_gdb_test "-var-show-format bar" \
	"\\^done,format=\"natural\"" \
	"show format variable bar"

# Test: c_variable-6.4
# Desc: value of variable bar
mi_gdb_test "-var-evaluate-expression bar" \
	"\\^done,value=\"2121\"" \
	"eval variable bar"

# Test: c_variable-6.5
# Desc: change format of bar to hex
mi_gdb_test "-var-set-format bar hexadecimal" \
	"\\^done,format=\"hexadecimal\",value=\"0x849\"" \
	"set format variable bar in hex"

# Test: c_variable-6.6
# Desc: value of bar with new format
mi_gdb_test "-var-evaluate-expression bar" \
	"\\^done,value=\"0x849\"" \
	"eval variable bar with new format"

# Test: c_variable-6.7
# Desc: change value of bar
mi_gdb_test "-var-assign bar 3" \
	"\\^done,value=\"0x3\"" \
	"assing to variable bar"

mi_gdb_test "-var-set-format bar decimal" \
	"\\^done,format=\"decimal\",value=\"3\"" \
	"set format variable bar in decimal"

mi_gdb_test "-var-evaluate-expression bar" \
	"\\^done,value=\"3\"" \
	"eval variable bar with new value"

mi_gdb_test "-var-delete bar" \
	"\\^done,ndeleted=\"1\"" \
	"delete var bar"

# Test: c_variable-6.11
# Desc: create variable foo
mi_create_varobj foo foo "create local variable foo"

# Test: c_variable-6.12
# Desc: type of variable foo
mi_gdb_test "-var-info-type foo" \
	"\\^done,type=\"int \\*\"" \
	"info type variable foo"

# Test: c_variable-6.13
# Desc: format of variable foo
mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"natural\"" \
	"show format variable foo in natural"

# Test: c_variable-6.14
# Desc: value of variable foo
mi_gdb_test "-var-evaluate-expression foo" \
	"\\^done,value=\"$hex\"" \
	"eval variable foo in natural"

# Test: c_variable-6.15
# Desc: change format of var to octal
mi_gdb_test "-var-set-format foo octal" \
	"\\^done,format=\"octal\",value=\"$octal\"" \
	"set format variable foo in octal"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"octal\"" \
	"show format variable foo in octal"

# Test: c_variable-6.16
# Desc: value of foo with new format
mi_gdb_test "-var-evaluate-expression foo" \
	"\\^done,value=\"\[0-7\]+\"" \
	"eval variable foo in octal"

# Test: c_variable-6.17
# Desc: change value of foo
mi_gdb_test "-var-assign foo 3" \
	"\\^done,value=\"03\"" \
	"assing to variable foo"

mi_gdb_test "-var-set-format foo decimal" \
	"\\^done,format=\"decimal\",value=\"3\"" \
	"set format variable foo decimal"

# Test: c_variable-6.18
# Desc: check new value of foo
mi_gdb_test "-var-evaluate-expression foo" \
	"\\^done,value=\"3\"" \
	"eval variable foo in decimal"


# Test: c_variable-6.19
# Desc: check optional format parameter of var-evaluate-expression
#       and check that current format is not changed
mi_gdb_test "-var-evaluate-expression -f hex foo" \
	"\\^done,value=\"0x3\"" \
	"eval variable foo in hex"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"decimal\"" \
	"show format variable foo after eval in hex"

mi_gdb_test "-var-evaluate-expression -f octal foo" \
	"\\^done,value=\"03\"" \
	"eval variable -f octal foo"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"decimal\"" \
	"show format variable foo after eval in octal"

mi_gdb_test "-var-evaluate-expression -f decimal foo" \
	"\\^done,value=\"3\"" \
	"eval variable -f decimal foo"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"decimal\"" \
	"show format variable foo after eval in decimal"

mi_gdb_test "-var-evaluate-expression -f nat foo" \
	"\\^done,value=\"0x3\"" \
	"eval variable -f nat foo"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"decimal\"" \
	"show format variable foo after eval in natural"

mi_gdb_test "-var-evaluate-expression -f bin foo" \
	"\\^done,value=\"11\"" \
	"eval variable foo in binary"

mi_gdb_test "-var-show-format foo" \
	"\\^done,format=\"decimal\"" \
	"show format variable foo after eval in binary"

mi_gdb_test "-var-delete foo" \
	"\\^done,ndeleted=\"1\"" \
	"delete var foo"

# Test: c_variable-6.21
# Desc: create variable weird and children
mi_create_varobj weird weird "create local variable weird"

mi_list_varobj_children weird {
        {weird.integer integer 0 int}
        {weird.character character 0 char}
        {weird.char_ptr char_ptr 1 "char \\*"}
        {weird.long_int long_int 0 "long"}
        {weird.int_ptr_ptr int_ptr_ptr 1 "int \\*\\*"}
        {weird.long_array long_array 10 "long \\[10\\]"}
        {weird.func_ptr func_ptr 0 "void \\(\\*\\)\\((void)?\\)"}
        {weird.func_ptr_struct func_ptr_struct 0 \
                 "struct _struct_decl \\(\\*\\)(\\(int, char \\*, long\\))?"}
        {weird.func_ptr_ptr func_ptr_ptr 0 \
                 "struct _struct_decl \\*\\(\\*\\)\\((int, char \\*, long)?\\)"}
        {weird.u1 u1 4 "union \\{\\.\\.\\.\\}"}
        {weird.s2 s2 4 "struct \\{\\.\\.\\.\\}"}
} "get children local variable weird"


# Test: c_variable-6.23
# Desc: change format of weird.func_ptr and weird.func_ptr_ptr
mi_gdb_test "-var-set-format weird.func_ptr hexadecimal" \
	"\\^done,format=\"hexadecimal\",value=\"$hex\"" \
	"set format variable weird.func_ptr in hex (1)"

mi_gdb_test "-var-show-format weird.func_ptr" \
	"\\^done,format=\"hexadecimal\"" \
	"show format variable weird.func_ptr"

mi_gdb_test "-var-set-format weird.func_ptr_ptr hexadecimal" \
	"\\^done,format=\"hexadecimal\",value=\"$hex\"" \
	"set format variable weird.func_ptr_ptr in hex"

mi_gdb_test "-var-show-format weird.func_ptr_ptr" \
	"\\^done,format=\"hexadecimal\"" \
	"show format variable weird.func_ptr_ptr"

# Test: c_variable-6.24
# Desc: format of weird and children
mi_gdb_test "-var-set-format weird natural" \
	"\\^done,format=\"natural\",value=\"$hex\"" \
	"set format variable weird"

mi_gdb_test "-var-set-format weird.integer natural" \
	"\\^done,format=\"natural\",value=\"123\"" \
	"set format variable weird.integer"

mi_gdb_test "-var-set-format weird.character natural" \
	"\\^done,format=\"natural\",value=\"0 '\\\\\\\\000'\"" \
	"set format variable weird.character"

mi_gdb_test "-var-set-format weird.char_ptr natural" \
	"\\^done,format=\"natural\",value=\"$hex \\\\\"hello\\\\\"\"" \
	"set format variable weird.char_ptr"

mi_gdb_test "-var-set-format weird.long_int natural" \
	"\\^done,format=\"natural\",value=\"0\"" \
	"set format variable weird.long_int"

mi_gdb_test "-var-set-format weird.int_ptr_ptr natural" \
	"\\^done,format=\"natural\",value=\"$hex\"" \
	"set format variable weird.int_ptr_ptr"

mi_gdb_test "-var-set-format weird.long_array natural" \
	"\\^done,format=\"natural\",value=\"\\\[10\\\]\"" \
	"set format variable weird.long_array"

mi_gdb_test "-var-set-format weird.func_ptr hexadecimal" \
	"\\^done,format=\"hexadecimal\",value=\"$hex\"" \
	"set format variable weird.func_ptr in hex (2)"

mi_gdb_test "-var-set-format weird.func_ptr_struct hexadecimal" \
	"\\^done,format=\"hexadecimal\",value=\"$hex\"" \
	"set format variable weird.func_ptr_struct"

mi_gdb_test "-var-set-format weird.func_ptr_ptr natural" \
	"\\^done,format=\"natural\",value=\"$hex <nothing2>\"" \
	"set format variable weird.func_ptr_ptr in natural"

mi_gdb_test "-var-set-format weird.u1 natural" \
	"\\^done,format=\"natural\",value=\"\{...\}\"" \
	"set format variable weird.u1"

mi_gdb_test "-var-set-format weird.s2 natural" \
	"\\^done,format=\"natural\",value=\"\{...\}\"" \
	"set format variable weird.s2"

# Test: c_variable-6.25
# Desc: value of weird and children
#gdbtk_test c_variable-6.25 {value of weird and children} {
#  set values {}
#  foreach v [lsort [array names var]] f [list x "" "" x x x x d d d d d] {
#    lappend values [value $v $f]
#  }

#  set values
#} {ok ok ok ok ok ok ok ok weird.long_array ok weird.s2 weird.u1}

# Test: c_variable-6.26
# Desc: change format of weird and children to octal
#gdbtk_test c_variable-6.26 {change format of weird and children to octal} {
#  set formats {}
#  foreach v [lsort [array names var]] {
#    $var($v) format octal
#    lappend formats [$var($v) format]
#  }

#  set formats
#} {octal octal octal octal octal octal octal octal octal octal octal octal}

# Test: c_variable-6.27
# Desc: value of weird and children with new format
#gdbtk_test c_variable-6.27 {value of foo with new format} {
#  set values {}
#  foreach v [lsort [array names var]] {
#    lappend values [value $v o]
#  }

#  set values
#} {ok ok ok ok ok ok ok ok weird.long_array ok weird.s2 weird.u1}

# Test: c_variable-6.30
# Desc: create more children of weird
#gdbtk_test c_variable-6.30 {create more children of weird} {
#  foreach v [array names var] {
#    get_children $v
#  }

#  # Do it twice to get more children
#  foreach v [array names var] {
#    get_children $v
#  }

#  lsort [array names var]
#} {weird weird.char_ptr weird.character weird.func_ptr weird.func_ptr_ptr weird.func_ptr_struct weird.int_ptr_ptr weird.int_ptr_ptr.*int_ptr_ptr weird.int_ptr_ptr.*int_ptr_ptr.**int_ptr_ptr weird.integer weird.long_array weird.long_array.0 weird.long_array.1 weird.long_array.2 weird.long_array.3 weird.long_array.4 weird.long_array.5 weird.long_array.6 weird.long_array.7 weird.long_array.8 weird.long_array.9 weird.long_int weird.s2 weird.s2.g weird.s2.h weird.s2.i weird.s2.i.0 weird.s2.i.1 weird.s2.i.2 weird.s2.i.3 weird.s2.i.4 weird.s2.i.5 weird.s2.i.6 weird.s2.i.7 weird.s2.i.8 weird.s2.i.9 weird.s2.u2 weird.s2.u2.f weird.s2.u2.u1s1 weird.s2.u2.u1s2 weird.u1 weird.u1.a weird.u1.b weird.u1.c weird.u1.d}

# Test: c_variable-6.31
# Desc: check that all children of weird change
#       Ok, obviously things like weird.s2 and weird.u1 will not change!
#gdbtk_test *c_variable-6.31 {check that all children of weird change (ops, we are now reporting array names as changed in this case - seems harmless though)} {
#  $var(weird) value 0x2121
#  check_update
#} {{weird.integer weird.character weird.char_ptr weird.long_int weird.int_ptr_ptr weird.int_ptr_ptr.*int_ptr_ptr weird.int_ptr_ptr.*int_ptr_ptr.**int_ptr_ptr weird.long_array.0 weird.long_array.1 weird.long_array.2 weird.long_array.3 weird.long_array.4 weird.long_array.5 weird.long_array.6 weird.long_array.7 weird.long_array.8 weird.long_array.9 weird.func_ptr weird.func_ptr_struct weird.func_ptr_ptr weird.u1.a weird.u1.b weird.u1.c weird.u1.d weird.s2.u2.f weird.s2.g weird.s2.h weird.s2.i.0 weird.s2.i.1 weird.s2.i.2 weird.s2.i.3 weird.s2.i.4 weird.s2.i.5 weird.s2.i.6 weird.s2.i.7 weird.s2.i.8 weird.s2.i.9} {weird.s2.i weird.s2.u2 weird weird.s2.u2.u1s1 weird.s2.u2.u1s2 weird.s2 weird.long_array weird.u1} {}}

mi_gdb_test "-var-delete weird" \
	"\\^done,ndeleted=\"12\"" \
	"delete var weird"


#####               #####
#                       #
# Special Display Tests #
#                       #
#####               #####

# Stop at the end of "do_special_tests"

set line_dst_incr_a_2 [gdb_get_line_number "incr_a(2);"]

mi_create_breakpoint "$line_dst_incr_a_2" \
    "break-insert operation 2" \
    -number 2 -func do_special_tests -file ".*var-cmd.c" \
    -line $line_dst_incr_a_2

mi_execute_to "exec-continue" "breakpoint-hit" "do_special_tests" "" \
    ".*var-cmd.c" $line_dst_incr_a_2 { "" "disp=\"keep\"" } \
    "continue to do_special_tests"

# Test: c_variable-7.10
# Desc: create union u
mi_create_varobj u u "create local variable u"

# Test: c_variable-7.11
# Desc: value of u
mi_gdb_test "-var-evaluate-expression u" \
	"\\^done,value=\"\{\\.\\.\\.\}\"" \
	"eval variable u"

# Test: c_variable-7.12
# Desc: type of u
mi_gdb_test "-var-info-type u" \
	"\\^done,type=\"union named_union\"" \
	"info type variable u"

# Test: c_variable-7.13
# Desc: is u editable
mi_gdb_test "-var-show-attributes u" \
	"\\^done,attr=\"noneditable\"" \
	"is u editable"

# Test: c_variable-7.14
# Desc: number of children of u
mi_gdb_test "-var-info-num-children u" \
	"\\^done,numchild=\"2\"" \
	"get number of children of u"

# Test: c_variable-7.15
# Desc: children of u
mi_list_varobj_children u {
        {u.integer integer 0 int}
        {u.char_ptr char_ptr 1 {char \*}}
} "get children of u"

# Test: c_variable-7.20
# Desc: create anonu
mi_create_varobj anonu anonu "create local variable anonu"

# Test: c_variable-7.21
# Desc: value of anonu
mi_gdb_test "-var-evaluate-expression anonu" \
	"\\^done,value=\"\{\\.\\.\\.\}\"" \
	"eval variable anonu"

# Test: c_variable-7.22
# Desc: type of anonu
mi_gdb_test "-var-info-type anonu" \
	"\\^done,type=\"union \{\\.\\.\\.\}\"" \
	"info type variable anonu"

# Test: c_variable-7.23
# Desc: is anonu editable
mi_gdb_test "-var-show-attributes anonu" \
	"\\^done,attr=\"noneditable\"" \
	"is anonu editable"

# Test: c_variable-7.24
# Desc: number of children of anonu
mi_gdb_test "-var-info-num-children anonu" \
	"\\^done,numchild=\"3\"" \
	"get number of children of anonu"

# Test: c_variable-7.25
# Desc: children of anonu
mi_list_varobj_children "anonu" {
        {anonu.a a 0 int}
        {anonu.b b 0 char}
        {anonu.c c 0 "long"}
} "get children of anonu"

# Test: c_variable-7.30
# Desc: create struct s
mi_create_varobj s s "create local variable s"


# Test: c_variable-7.31
# Desc: value of s
mi_gdb_test "-var-evaluate-expression s" \
	"\\^done,value=\"\{\\.\\.\\.\}\"" \
	"eval variable s"

# Test: c_variable-7.32
# Desc: type of s
mi_gdb_test "-var-info-type s" \
	"\\^done,type=\"struct _simple_struct\"" \
	"info type variable s"

# Test: c_variable-7.33
# Desc: is s editable
mi_gdb_test "-var-show-attributes s" \
	"\\^done,attr=\"noneditable\"" \
	"is s editable"

# Test: c_variable-7.34
# Desc: number of children of s
mi_gdb_test "-var-info-num-children s" \
	"\\^done,numchild=\"6\"" \
	"get number of children of s"

# Test: c_variable-7.35
# Desc: children of s
mi_list_varobj_children s {
        {s.integer integer 0 int}
        {s.unsigned_integer unsigned_integer 0 "unsigned int"}
        {s.character character 0 char}
        {s.signed_character signed_character 0 "signed char"}
        {s.char_ptr char_ptr 1 {char \*}}
        {s.array_of_10 array_of_10 10 {int \[10\]}}
} "get children of s"
#} {integer unsigned_integer character signed_character char_ptr array_of_10}

# Test: c_variable-7.40
# Desc: create anons
mi_create_varobj anons anons "create local variable anons"

# Test: c_variable-7.41
# Desc: value of anons
mi_gdb_test "-var-evaluate-expression anons" \
	"\\^done,value=\"\{\\.\\.\\.\}\"" \
	"eval variable anons"

# Test: c_variable-7.42
# Desc: type of anons
mi_gdb_test "-var-info-type anons" \
	"\\^done,type=\"struct \{\\.\\.\\.\}\"" \
	"info type variable anons"

# Test: c_variable-7.43
# Desc: is anons editable
mi_gdb_test "-var-show-attributes anons" \
	"\\^done,attr=\"noneditable\"" \
	"is anons editable"

# Test: c_variable-7.44
# Desc: number of children of anons
mi_gdb_test "-var-info-num-children anons" \
	"\\^done,numchild=\"3\"" \
	"get number of children of anons"

# Test: c_variable-7.45
# Desc: children of anons
mi_list_varobj_children anons {
        {anons.a a 0 int}
        {anons.b b 0 char}
        {anons.c c 0 "long"}
} "get children of anons"

# Test: c_variable-7.50
# Desc: create enum e
mi_create_varobj e e "create local variable e"

# Test: c_variable-7.51
# Desc: value of e
mi_gdb_test "-var-evaluate-expression e" \
	"\\^done,value=\"bar\"" \
	"eval variable e"

# Test: c_variable-7.52
# Desc: type of e
mi_gdb_test "-var-info-type e" \
	"\\^done,type=\"enum foo\"" \
	"info type variable e"

# Test: c_variable-7.53
# Desc: is e editable
mi_gdb_test "-var-show-attributes e" \
	"\\^done,attr=\"editable\"" \
	"is e editable"

# Test: c_variable-7.54
# Desc: number of children of e
mi_gdb_test "-var-info-num-children e" \
	"\\^done,numchild=\"0\"" \
	"get number of children of e"

# Test: c_variable-7.55
# Desc: children of e
mi_gdb_test "-var-list-children e" \
	"\\^done,numchild=\"0\",has_more=\"0\"" \
	"get children of e"

# Test: c_variable-7.60
# Desc: create anone
mi_create_varobj anone anone "create local variable anone"

# Test: c_variable-7.61
# Desc: value of anone
mi_gdb_test "-var-evaluate-expression anone" \
	"\\^done,value=\"A\"" \
	"eval variable anone"

# Test: c_variable-7.70
# Desc: create anone
mi_gdb_test "-var-create anone * anone" \
	"\\^error,msg=\"Duplicate variable object name\"" \
	"create duplicate local variable anone"


# Test: c_variable-7.72
# Desc: type of anone
mi_gdb_test "-var-info-type anone" \
	"\\^done,type=\"enum \{\\.\\.\\.\}\"" \
	"info type variable anone"


# Test: c_variable-7.73
# Desc: is anone editable
mi_gdb_test "-var-show-attributes anone" \
	"\\^done,attr=\"editable\"" \
	"is anone editable"

# Test: c_variable-7.74
# Desc: number of children of anone
mi_gdb_test "-var-info-num-children anone" \
	"\\^done,numchild=\"0\"" \
	"get number of children of anone"

# Test: c_variable-7.75
# Desc: children of anone
mi_gdb_test "-var-list-children anone" \
	"\\^done,numchild=\"0\",has_more=\"0\"" \
	"get children of anone"


# Record fp
if ![mi_gdb_test "p/x \$fp" ".*($hex).*\\^done" "print FP register"] {
    set fp $expect_out(3,string) 
}

mi_continue_to "incr_a"

# Test: c_variable-7.81
# Desc: Create variables in different scopes
mi_gdb_test "-var-create a1 * a" \
	"\\^done,name=\"a1\",numchild=\"0\",value=\".*\",type=\"char\".*" \
	"create local variable a1"

if { [info exists fp] } {
    mi_gdb_test "-var-create a2 $fp a" \
	"\\^done,name=\"a2\",numchild=\"0\",value=\".*\",type=\"int\".*" \
	"create variable a2 in different scope"
} else {
    untested "create variable a2 in different scope"
}

#gdbtk_test c_variable-7.81 {create variables in different scopes} {
#  set a1 [gdb_variable create -expr a]
#  set a2 [gdb_variable create -expr a -frame $fp]

#  set vals {}
#  lappend vals [$a1 value]
#  lappend vals [$a2 value]
#  set vals
#} {2 1}


mi_gdb_exit
return 0
